package com.cloudspace.yunloan.mediarecorder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.activity.YunloanActivity;
import com.cloudspace.yunloan.player.DefaultVideoPlayerSurfaceView;
import com.cloudspace.yunloan.player.base.AbsMediaPlayer;

public class VideoPlayerActivity extends Activity implements View.OnClickListener {
    private FrameLayout mRoot;
    private ImageView closeImg;
    private DefaultVideoPlayerSurfaceView mVideoView;
    private String mPlayUrl;

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);

        mRoot = findViewById(R.id.root);
        closeImg = findViewById(R.id.close);
        closeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mVideoView != null){
                    mVideoView.stop();
                }
                finish();
            }
        });


        mPlayUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
        mVideoView = DefaultVideoPlayerSurfaceView.getInstance(this, AbsMediaPlayer.TYPE_EXO);
        if (getIntent() != null && getIntent().getExtras() != null){
            mPlayUrl = getIntent().getExtras().getString("play_url");
        }

        DefaultVideoPlayerSurfaceView.getInstance(this).setVideoURI(mPlayUrl);
        mRoot.removeAllViews();
        mRoot.addView(mVideoView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mVideoView.setAspectMode(0);
    }

    public static void showActivity(Context context, String playUrl){
        Intent intent = new Intent(context, VideoPlayerActivity.class);

        if (!TextUtils.isEmpty(playUrl)){
            Bundle bundle = new Bundle();
            bundle.putString("play_url", playUrl);
            intent.putExtras(bundle);

            if (!(context instanceof Activity)){
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        }

        context.startActivity(intent);
    }
}
